package com.ldg.seckill.controller;

import com.alipay.api.AlipayApiException;
import com.ld.pojo.PayAsyncVo;
import com.ldg.seckill.client.OrderClient;
import com.ldg.seckill.service.TbSeckillOrderService;
import org.jasig.cas.client.util.AbstractCasFilter;
import org.jasig.cas.client.validation.Assertion;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

/**
 * @Author: Liudao
 * @Date: 2022-10-22-14:59
 * @Description:
 */
@Controller
@RequestMapping("seckillOrder")
public class SecKillOrderController {

    @Autowired
    private TbSeckillOrderService orderService;
    @Autowired
    private OrderClient orderClient;
    //抢单
    @RequestMapping("saveOrder/{id}")
    public String saveOrder(@PathVariable String id, HttpSession session, Model model){
        //得到登录名
        Assertion assertion = (Assertion) session.getAttribute(AbstractCasFilter.CONST_CAS_ASSERTION);
        if(assertion != null) {
            //得到用户名
            String name = assertion.getPrincipal().getName();
            //将订单号放到model中
            model.addAttribute("outTradeNo",id);
            //调用抢单方法进行抢单
            orderService.saveOrder(name,id);
        }
        // 到达支付页面
        return "pay";
    }

    //远程同步调用
    @GetMapping("orderlist")
    public String orderList(Model model,HttpSession session){
        return orderClient.orderList(model,session);
    }

    //远程异步调用
    @PostMapping("payed/notify")
    public String payedNotify(PayAsyncVo vo, HttpServletRequest request) throws AlipayApiException {
        return orderClient.payedNotify(vo,request);
    }
}
